<template>
  <div class="edit-adress">
    <head-top head-title='添加地址' go-back="true"></head-top>
    <section class="content">
      <div class="same">
        <span>收货人：</span>
        <input type="text" placeholder="请输入姓名（20个字以内）">
      </div>
      <div class="same">
        <span>手机号：</span>
        <input type="text" placeholder="请输入联系人手机号">
      </div>
      <div class="address" @click="mask">
        <span>地址：</span>
        <span class="city">{{city}}</span>
        <div class="icon">
          <span class="go-icon"></span>
        </div>
      </div>
      <div class="same" @click="detailAddress">
        <span>详细地址：</span>
        <input type="text" placeholder="请输入你的详细地址">
      </div>
    </section>
    <section class="footer" @click="goSelectAddress">
      <span>保存地址</span>
    </section>
    <mt-popup position="bottom" v-model="popupVisible">
      <div class="sure-cancle">
        <span>取消</span>
        <span @click="sure">确认</span>
      </div>
      <mt-picker :slots="addressSlots" class="picker" @change="onAddressChange"></mt-picker>
    </mt-popup>
    <div class="slider">
      你好
      <select name="" id="">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
      </select>
    </div>
  </div>
</template>

<script>
import headTop from '../header/Header.vue'
import address from '../../../static/city.json'
import { Toast } from 'mint-ui'
export default {
  name: 'EditAdress',
  data() {
    return {
      value: '',
      xianObj: {},
      popupVisible: false,
      addressSlots: [
        {
          flex: 1,
          defaultIndex: 0,
          values: Object.keys(address),
          className: 'slot1',
          textAlign: 'center'
        },
        {
          divider: true,
          content: '-',
          className: 'slot2'
        },
        {
          flex: 1,
          values: [''],
          className: 'slot3',
          textAlign: 'center'
        },
        {
          divider: true,
          content: '-',
          className: 'slot4'
        },
        {
          flex: 1,
          values: [''],
          className: 'slot5',
          textAlign: 'center'
        }
      ],
      addressProvince: '省',
      addressCity: '市',
      addressXian: '区',
      city: '',
      dataList: [],
      dataObj: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.addressSlots[0].defaultIndex = 0
      }, 100)
    })
  },
  created() {
    // console.log(this.city)
    this.getDataList()
  },
  methods: {
    getDataList() {
      const url = 'https://ynwx.zgwyzxw.cn/index.php/home/Proprietorauth/getHouses?areas_id=204'
      this.$http.get(url).then(res => {
        this.dataList = res.data.data
        console.log(this.dataList)
        this.dataObj = Object.assign({}, this.dataList)
        console.log(this.dataObj)
      })
    },
    mask() {
      this.popupVisible = true
    },
    sure() {
      this.popupVisible = false
      console.log(this.city)
    },
    detailAddress() {
      if (this.city === '') {
        Toast({
          message: '请选择地址',
          position: 'middle',
          duration: 1000
        })
        return false
      }
    },
    onAddressChange(picker, values) {
      this.value = values[0]
      if (typeof (values[0]) === 'undefined') {
        console.log('省份为空')
        return
      }
      let shi = Object.keys(address[values[0]])
      console.log(values[1])
      let index = shi.indexOf(values[1])
      // if (index === -1) {
      //   index = 0
      // }
      let xian = address[values[0]][shi[index]]
      this.xianObj = xian
      picker.setSlotValues(1, shi)
      this.addressProvince = values[0]
      this.addressCity = values[1]
      this.addressXian = values[2]
      this.city = this.addressProvince + ' ' + (this.addressCity ? this.addressCity : ' ') + ' ' + (this.addressXian ? this.addressXian : ' ')
      picker.setSlotValues(2, xian)
    },
    goSelectAddress() {
      this.$router.push({path: '/home/SelectAddress'})
    }
  },
  components: {
    headTop
  }
}
</script>

<style lang='scss'>
@import "../../common/sass/index.scss";
.edit-adress {
  background-color: #fff;
  .content {
    padding-top: 40px;
    margin: 0 10px;
    .same,
    .address {
      height: 44px;
      line-height: 44px;
      font-size: 15px;
      border-bottom: 1px solid #f1f1f1;
      input {
        outline: none;
        border: none;
      }
    }
    .same:last-child {
      border-bottom: 1px solid transparent;
    }
    .address {
      .city {
        font-size: 12px;
      }
      input:disabled {
        background-color: #fff;
      }
      input::-webkit-input-placeholder {
        color: #0078de;
      }
      input:-moz-placeholder {
        color: #0078de;
      }
      input::-moz-placeholder {
        color: #0078de;
      }
      input:-ms-input-placeholder {
        color: #0078de;
      }
      .icon {
        width: 44px;
        height: 44px;
        float: right;
        text-align: right;
        .go-icon {
          display: inline-block;
          width: 8px;
          height: 14px;
          background-size: 8px 14px;
          background-repeat: no-repeat;
          @include bgimg("./images/fanhui1");
          vertical-align: middle;
        }
      }
    }
  }
  .footer{
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
    height: 44px;
    line-height: 44px;
    font-size: 15px;
    background-color: #0078de;
    color: #fff;
    text-align: center;
    span{
      display: block;
      margin: 0 auto;
    }
  }
}
.sure-cancle{
  border-bottom: 1px solid #f1f1f1;
  padding: 15px 10px;
  span{
    font-size: 15px;
    &:nth-of-type(2){
      color: #0078de;
      float: right;
    }
  }
}
.mint-popup{
  width: 100%;
}
.picker-item{
  font-size: 15px;
}
.picker-item.picker-selected{
  color:#0078de;
}
</style>
